package com.besttone.action;

import com.besttone.bean.IntergrationDayInfo;
import com.besttone.enums.EnumAPIResultCode;
import com.besttone.enums.EnumBusiType;
import com.besttone.util.JsonUtil;
import com.besttone.util.Sms2SolrUtil;
import com.jfinal.core.Controller;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 离线按天综合查询总数接口(短信数、短链接访问数、消息数...)
 * Created by zhengzhenzhen on 2016/11/22.
 */
public class IntegrationCountByDayAction extends Controller {
    private static Log _log = LogFactory.getLog(IntegrationCountByDayAction.class);

    public void queryAllCountByDay() {
        String send_parent_account="cm_mpqg";//TODO default  bstjm_gj
        String streaming_no = getPara("streaming_no");
        setAttr("streaming_no", streaming_no);
        String sp_account = getPara("sp_account");
        String intergration_info = getPara("intergration_info");
        String day_id_from = getPara("day_id_from");// 开始：按天
        String day_id_to = getPara("day_id_to");//结束：按天
        _log.info("请求查询参数:streaming_no=" + streaming_no + ",sp_account="
                + sp_account + ",intergration_info=" + intergration_info);
        if (StringUtils.isBlank(intergration_info)) {
            setAttr("result_code",
                    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            setAttr("result_msg", "集成查询信息参数不能为空");
            renderJson();
            return;
        }
        List<Object> infoList = JsonUtil.jsonStringToObject(intergration_info, List.class);
        List<IntergrationDayInfo> resultInfoList = new ArrayList<IntergrationDayInfo>();
        for (Object obj : infoList) {
            IntergrationDayInfo info = JsonUtil.jsonStringToObject(
                    obj.toString(), IntergrationDayInfo.class);
            /**if (StringUtils.isBlank(info.getTemplate_code())) {
                setAttr("result_code",
                        EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
                setAttr("result_msg", "template_code不能为空");
                renderJson();
                return;
            }**/
            if (StringUtils.isBlank(day_id_from)) {
                setAttr("result_code",
                        EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
                setAttr("result_msg", "day_id_from不能为空");
                renderJson();
                return;
            }
            if (StringUtils.isBlank(day_id_to)) {
                setAttr("result_code",
                        EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
                setAttr("result_msg", "day_id_to不能为空");
                renderJson();
                return;
            }
            String baseStr=getDayFromAndTo(send_parent_account,day_id_from,day_id_to);
            // query body
            StringBuffer strBufSms = new StringBuffer();
            strBufSms.append(baseStr);
            if (StringUtils.isNotBlank(info.getTemplate_code())) {
                strBufSms.append(" AND template_code:" + info.getTemplate_code());
            }
            if (StringUtils.isNotBlank(info.getSend_number())) {
                strBufSms.append(" AND send_number:" + info.getSend_number());
            }
            String querySql = strBufSms.toString();
            _log.info("smsday collection查询sms:" + querySql);
            try {
                //TODO 可能满足不了需求,因为每个busi_type查询的条件不同，不都是按照template_code或send_number查询的
                Map<String,Long> sumMap=Sms2SolrUtil.searchSumByDay(querySql);
                info.setSms_count(sumMap.get(EnumBusiType.SMS.getName()));
                info.setShortlink_count(sumMap.get(EnumBusiType.SHORT_LINK.getName()));
                info.setMsg_count(sumMap.get(EnumBusiType.COUPON_OR_RESERVE.getName()));
                //TODO 参考 IntegrationCountAction 修改
               // Sms2SolrUtil.searchSumByBusinessTypeAndDay();
            } catch (Exception e) {
                _log.info("solr查询异常:" + e);
                setAttr("result_code",
                        EnumAPIResultCode.SYSTEM_ERROR.getValue());
                setAttr("result_msg",
                        EnumAPIResultCode.SYSTEM_ERROR.getName());
            }
            // query body end
            resultInfoList.add(info);
        }
        // return
        setAttr("intergration_info",
                JsonUtil.ObjectToJsonString(resultInfoList));
        setAttr("result_code", EnumAPIResultCode.SUCCESS.getValue());
        setAttr("result_msg", EnumAPIResultCode.SUCCESS.getName());
        renderJson();
        _log.info("return value:" + getAttr("intergration_info"));
        _log.info("query intergration success");
        return;
    }

    /**
     * 拼接基本必填查询语句
     * @param send_parent_account
     * @param day_id_from
     * @param day_id_to
     * @return
     */
    private String getDayFromAndTo(String send_parent_account,String day_id_from,String day_id_to){
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("send_parent_account:" + send_parent_account);
        // 精确到天
        if (StringUtils.isNotBlank(day_id_from)) {
            day_id_from = StringUtils.trim(day_id_from);
            strBuf.append(" AND (day_id:[");
            strBuf.append(day_id_from);
            strBuf.append(" TO ");
            if (StringUtils.isNotBlank(day_id_to)) {
                strBuf.append(StringUtils.trim(day_id_to));
            } else {
                strBuf.append("*");
            }
            strBuf.append("]) ");
        } else if (StringUtils.isNotBlank(day_id_to)) {
            day_id_to = StringUtils.trim(day_id_to);
            strBuf.append(" AND (day_id:[* TO ");
            strBuf.append(StringUtils.trim(day_id_to));
            strBuf.append("]) ");
        }
        return strBuf.toString();
    }
}
